import React from 'react'

import {WeaTools} from 'ecCom';
let Base64 = WeaTools.Base64;


export default class ContainerViewProxy extends React.Component{

    componentWillMount(){
        const {onLoad} = this.props
        const {data} = this.props.state
        const {id,setting} = data
        const {needRefresh,refreshMins} = setting
        window.portalTs.element.workflow.onloads[id] = onLoad
        if(needRefresh == '1' && refreshMins > 0){
            setInterval(onLoad,refreshMins*60*1000)
        }


    }

    componentWillUnmount(){
        const {data} = this.props.state
        const {id} = data
        if(window.portalTs.element.workflow.onloads[id] && typeof window.portalTs.element.workflow.onloads [id] == 'function'){
            delete window.portalTs.element.workflow.onloads[id]
        }
    }

    render(){
        const {ContainerView} = this.props
        const {data,tabId} = this.props.state
        const {items,setting,id} = data

        //有more页面地址元素
        let moreUrl = ""
        let json ={}
        if(items.length > 0 && items.find((element)=>(element.id == tabId))){
            json = {...items.find((element)=>(element.id == tabId)).setting}
            json.perpage = setting.showNumber
            // delete json.title
            if(json.viewType ==6){
                moreUrl = "/spa/workflow/index_mobx.jsp#/main/workflow/supervise?fromwhere=jsonFilter&jsonstr="+'&eid=portal_ts_'+id+'&tabid='+tabId+'&tabTitle='+Base64.encodeURI(json.title)
            }else{
                moreUrl = "/spa/workflow/index_mobx.jsp#/main/workflow/queryFlowResult?fromwhere=jsonFilter&jsonstr="+'&eid=portal_ts_'+id+'&tabid='+tabId+'&tabTitle='+Base64.encodeURI(json.title)
            }
        }

        // //没有more页面地址元素，需要在setting 中设配置项
        // let moreUrl = ""
        // if(items.length > 0){
        //     moreUrl = items[tabId].setting.moreUrl
        // }
        return (
            <ContainerView ecId={`${this && this.props && this.props.ecId || ''}_ContainerView@4mxblr`} ref='ContainerView' {...this.props} moreUrl={moreUrl}></ContainerView>
        )
    }
}

window.portalTs = (window.portalTs && window.portalTs.element) ? (
    {
        element:{
            ...window.portalTs.element,
            workflow:{
                onloads:{},
                reflush: function(){
                    if(window.portalTs.element.workflow.onloads  && typeof window.portalTs.element.workflow.onloads  == 'object') {
                        for (let i in window.portalTs.element.workflow.onloads) {
                            if (typeof window.portalTs.element.workflow.onloads [i] == 'function') {
                                window.portalTs.element.workflow.onloads[i]()
                            }
                        }
                    }
                }
            }

        }
    }
) : (
    {
        element:{
            workflow:{
                onloads:{},
                reflush: function(){
                    if(window.portalTs.element.workflow.onloads  && typeof window.portalTs.element.workflow.onloads  == 'object') {
                        for (let i in window.portalTs.element.workflow.onloads) {
                            if (typeof window.portalTs.element.workflow.onloads [i] == 'function') {
                                window.portalTs.element.workflow.onloads[i]()
                            }
                        }
                    }
                }
            }
        }
    }
)